import QtQuick 1.1
import QtMobility.gallery 1.1
//import com.nokia.symbian 1.0
import com.nokia.meego 1.0
import com.nokia.extras 1.0
import Qt.labs.folderlistmodel 1.0

import REngine 1.0

RPage {
    id: fileListView
    function stripFilePath(path) {
        console.log("stripFilePath():path:", path)
        var filePath = new String(path)
        return filePath.replace("/home/user/MyDocs", "")
    }

    function stripFilePrefix(path) {
        console.log("stripFilePrefix():path:", path)
        var filePath = new String(path)
        return filePath.replace("file://", "")
    }

    function stripSuffix(path) {
        console.log("stripSuffix():path:", path)
        var filePath = new String(path)
        return filePath.substring(0, filePath.lastIndexOf("/"))
    }

    function setCurrentFolder(folder) {
        folderModel.folder = folder
        engine.setSetting(REngine.LastReadPath, stripFilePrefix(folderModel.folder))
    }

    function filType(path) {
        console.log("filType():path:", path)
        var filePath = new String(path)
        var suffix = filePath.substring(filePath.lastIndexOf(".")).toLowerCase()
        console.log("suffix:", suffix)
        if (suffix === ".txt") {
            console.log("file is txt")
            return 1
        }
        else if (suffix === ".umd") {
            console.log("file is txt")
            return 2
        }
        else {
            console.log("file is folder")
            return 0
        }
    }

    Rectangle {
        id: currentPath
        anchors.top: parent.top
        width: parent.width
        height: 70
        color: "steelblue"
        Label {
            anchors.fill: parent
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            color: "white"
            text: stripFilePrefix(folderModel.folder)
        }
    }

    ListView {
        id: list
        clip: true
        anchors.top: currentPath.bottom
        anchors.bottom: parent.bottom
        width: parent.width
        model: folderModel
        spacing: 2
        section.property: "path"
        delegate:  RListDelegate {
            folder: folderModel.isFolder(index)
            iconType: filType(decodeURI(filePath))
            title: decodeURI(fileName)
            size: engine.fileSize(stripFilePrefix(decodeURI(filePath/*path*/)))
            onClicked: {
                console.log("item clicked", index, fileName, filePath, decodeURI(filePath))
                //                engine.openFile(path+"/"+model.fileName)
                if (folderModel.isFolder(index)) {
                    console.log("clicked item is folder, look into the folder", filePath)
                    setCurrentFolder(filePath)
                }
                else {
                    console.log("clicked item is text, open it")
                    setCurrentFolder(folderModel.folder)
                    fileListView.pageStack.push(Qt.resolvedUrl("Viewer.qml"),
                                                {fileName: stripFilePrefix(decodeURI(filePath)),
                                                    window: fileListView.window,
                                                    orientationLock: fileListView.orientationLock})
                }
            }
        }
    }

    SectionScroller {
        listView: list
    }

    ScrollDecorator { flickableItem: list }

    FolderListModel {
        id: folderModel
//        folder:
        nameFilters: [ "*.txt", "*.TXT", "*.umd" ]
        sortField: "Type"
    }
    onStatusChanged:  {
        if (status == PageStatus.Active) {
            console.log("set current path")
            var lastPath = engine.setting(REngine.LastReadPath)
            if (engine.pathExist(lastPath)) {
                setCurrentFolder(lastPath)
            }
            else {
                setCurrentFolder("/home/user/MyDocs")
            }
        }else if (status == PageStatus.Activating) {
            screen.allowedOrientations = engine.setting(REngine.Orientation)
        }
    }

    DocumentGalleryModel {
        id: galleryModel
        rootType: DocumentGallery.Text
        properties: [ "fileName", "path", "filePath" ]
        sortProperties: [ "filePath" ]
        autoUpdate: true
        filter: GalleryFilterIntersection {
            filters: [
                GalleryFilterUnion {
                    filters: [
                        GalleryWildcardFilter {
                            property: "fileName";
                            value: "*.TXT";
                        },
                        GalleryWildcardFilter {
                            property: "fileName";
                            value: "*.txt";
                        }
                    ]
                },
                GalleryStartsWithFilter {
                    property: "path"
                    value: "/home/user/MyDocs/"
                }

            ]
        }
        Component.onCompleted: {
            console.log("model count", galleryModel.count)
        }
    }

    Component.onCompleted: {
        console.log("fileListView loaded")
        screen.allowedOrientations = engine.setting(REngine.Orientation)
    }

    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back"
            onClicked: {
                console.log("Back button clicked, current folder:", folderModel.folder)
                fileListView.pageStack.pop()
            }
        }

        ToolIcon {
            iconId: "toolbar-up"
            onClicked: {
                console.log("Up button clicked, current folder:", folderModel.folder)
                if (stripFilePrefix(folderModel.folder) !== "/") {
                    setCurrentFolder(stripFilePrefix(stripSuffix(folderModel.folder)))
                    console.log("changed folder:", folderModel.folder)
                }
            }
        }
    }

    //    onStatusChanged: {
    //        //reload orientation
    //        fileListView.orientationLock = engine.setting(REngine.Orientation)
    //    }
//    Connections {
//        target: platformWindow
//        onViewModeChanged: {
//            console.log("viewModeChanged", platformWindow.viewMode)
//            COM.handleViewModeVisibleChanged()
//        }
//        onVisibleChanged: {
//            console.log("visibleChanged", platformWindow.visible)
//            COM.handleViewModeVisibleChanged()
//        }
//    }
}


